/* Clear all previous data from memory */
clear all

/* Load the dataset for rejected firms' PSM (Propensity Score Matching) */
use "$output\rejected_psm.dta"

/* Keep only observations within the study period (2014-2019) */
keep if mdate < mofd(date("20200101","YMD"))
keep if mdate > mofd(date("20131231","YMD"))

/* Keep only firms within the event window of -12 to +24 months */
keep if deltamonths >= -12
keep if deltamonths <= 24

/* Generate financial variables */
gen loan = stloan + ltloan + bm + bi + oc   /* Total loan amount */
gen leasing = bm + bi                       /* Leasing component */
gen lloan = log(loan)                        /* Log of total loan */
gen lstloan = log(stloan)                    /* Log of short-term loan */
gen lavailable = log(oc)                     /* Log of available credit */
gen ldrawn = log(stloan + ltloan + leasing)  /* Log of drawn credit */
gen lleasing = log(leasing)                  /* Log of leasing */
gen clusage = co / (co + oc)                 /* Credit line usage ratio */
gen lltloan = log(ltloan + bm + bi)          /* Log of long-term loan */
gen lco = log(co)                            /* Log of credit line */
gen lother = log(stloan + oc)                /* Log of other loans */

/* TREND RELATIVE Coefficient Plot (Figure 3b) */
preserve

/* Convert deltamonths into a non-negative categorical variable */
replace delta = deltamonths + 12

/* Ensure sorted order */
sort siren delta
capture drop nb
bys siren delta: gen nb = _N
su nb, d
drop nb

/* Define labels for deltamonths categories */
capture lab_delta drop 
forvalues n = 0(1)36 {
    local displayed_value = `n' - 12
    dis "`displayed_value'"
    label define lab_delta `n' "`displayed_value'", add
}
label val delta lab_delta

/* Generate trend coefficient plots for loan variable */
foreach var in lloan { 
    /* Regression with fixed effects and clustering at firm level */
    qui eststo `var' : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

	 /* Figure 3b */
    /* Generate coefficient plot */
    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.2(0.1)0.3) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

    /* Save the graphs */
    graph save "$figures/Figure_3b_Trend_relative_`var'_coefplot_psm_rejected.gph", replace
    graph export "$figures/Figure_3b_Trend_relative_`var'_coefplot_psm_rejected.pdf", replace

    /* Second regression with different fixed effects */
    qui eststo `var'2 : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren mdate) cluster(siren)  

	/* Figure D2b */
    /* Generate second coefficient plot */
    coefplot (`var'2, ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.2(0.1)0.3) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

    /* Save second set of graphs */
    graph save "$figures/Figure_D2b_Trend_relative_`var'_coefplot_psm_rejected2.gph", replace
    graph export "$figures/Figure_D2b_Trend_relative_`var'_coefplot_psm_rejected2.pdf", replace
}

	 /* Figure 4a-b, right + Figure C2b */
/* Generate trend coefficient plots for additional variables */
foreach var in lco lltloan lother lstloan lavailable ldrawn lleasing clusage {
    qui eststo `var' : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren#i.year mdate) cluster(siren)  

    coefplot (`var', ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xlabel(2 "-11" 4 "-9" 6 "-7" 8 "-5" 10 "-3" 12 "-1" 14 "1" 16 "3" 18 "5" 20 "7" 22 "9" 24 "11" 26 "13" 28 "15" 30 "17" 32 "19" 34 "21" 36 "23", labsize(small)) ///
    xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.3(0.1)0.4) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

    graph save "$figures/Trend_relative_`var'_coefplot_psm_rejected.gph", replace
    graph export "$figures/Trend_relative_`var'_coefplot_psm_rejected.pdf", replace

    /* Second regression with different fixed effects */
    qui eststo `var'2 : reghdfe `var' i1.accepted##ib11.delta [pweight=weight], a(i.siren mdate) cluster(siren)  

    coefplot (`var'2, ci(95) mcolor(emerald%60) msize(medsmall) lwidth(medthin) ///
    ciopts(recast(rcap) color(gs7%90) fcolor(gs7%90) lpattern(dash))), ///
    omitted base keep(1.accepted#*) rename(1.accepted#*= "") vertical yline(0, lpattern(shortdash) lcolor(grey)) ///
    xsize(6) ysize(4) ylabel(, nogrid) ylabel(-0.3(0.1)0.4) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(color(white)) xline(12) ytitle("")

    graph save "$figures/Trend_relative_`var'_coefplot_psm_rejected2.gph", replace
    graph export "$figures/Trend_relative_`var'_coefplot_psm_rejected2.pdf", replace
}
restore
